﻿using System.Text.RegularExpressions;
using Magellan.Utilities;

namespace Magellan.Routing
{
    public class RegexConstraint : IRouteConstraint
    {
        private readonly Regex _regex;

        public RegexConstraint(string regex)
        {
            Guard.ArgumentNotNullOrEmpty(regex, "regex");
            _regex = new Regex(regex, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
        }

        public RegexConstraint(Regex regex)
        {
            Guard.ArgumentNotNull(regex, "regex");
            _regex = regex;
        }

        public bool Match(IRoute route, string value, string parameterName)
        {
            return _regex.Match(value).Success;
        }
    }
}
